package com.feicheng.lanniao.controller.upload;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.result.ResultInfo;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.HashMap;
import java.util.Map;


/**
 * @Author tianchunlei
 * @Des //图片上传接口类
 * @Date 2021/9/23
 **/
@RestController
@RequestMapping("/lanniao-user/image")
public class ImageUpLoad {

    public static final String SECRETID = "AKIDfBTWJq79vRhhRwEH5f2B7oGSO5WieKkv";
    public static final String SECRET_KEY = "ua1D9qHSN7f2A4kALIyPZT6WjhPr2xd3";
    public static final String REGION_NAME = "ap-shanghai";
    public static final String BUCKET = "lanniao-1307372768";
    /**
     * @return
     * @Author tianchunlei
     * @Description //图片上传接口
     * @Date 2021/9/23 11:26
     * @Param file:file module:上传需要携带module参数，接口需要module的值为car或house，接口会根据module上传到腾讯云的指定文件夹
     */
    @RequestMapping("/upload")
    public ResultInfo imageUpLoad(MultipartFile file, String module) {
        //参数校验
        if (file == null || module == null) {
            throw new SystemException("参数不能为空");
        }
        // 1 初始化用户身份信息（secretId, secretKey）。
        COSCredentials cred = new BasicCOSCredentials(SECRETID, SECRET_KEY);
        // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(REGION_NAME);
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        // 4 指定要上传的文件

        File localFile = null;
        try {
            localFile = File.createTempFile("temp",null);
            file.transferTo(localFile);
        } catch (Exception e) {
            throw new SystemException("图片上传失败");
        }
        // 5 指定文件将要存放的存储桶
        // 6 指定文件上传到 COS 上的路径，即对象键。例如对象键为folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        //构建日期路径
        String timeFolder = new DateTime().toString("/yyyy/MM/dd/");
        String key = module + timeFolder + IdUtil.simpleUUID() + file.getOriginalFilename();
        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        /*
        https://lanniao-1307372768.cos.ap-shanghai.myqcloud.com/car/2021/09/23/4177e4ba0c4f4097b5bc62685d31c8fffront.jpeg
         */
        // 关闭客户端(关闭后台线程)
        cosClient.shutdown();

        String url = "https://"+BUCKET+".cos."+REGION_NAME+".myqcloud.com/"+key;
        Map<String,String> map = new HashMap<>();
        map.put("URL",url);
        return ResultInfo.success(map);
    }
}